<?php
namespace Home\Controller;

class CartController extends CommonController {

	function _initialize() {
		parent::_initialize();	
		header("Content-Type:text/html; charset=utf-8");
		$this->cart = new \Com\Cart ();
	}

	//购物车增加，
	public function cartadd(){
		$Food=M('Food');
		$data['fid']=I('id');
		$fooditem=$Food->where($data)->find();
		$status = $this->cart->insert (
			array (
				'id' => $fooditem['fid'],
				'name' => $fooditem['fname'],
				'qty' => 1,
				'price' => $fooditem['fprice'],
				'sid' => $fooditem['sid'],
				'fcid' => $fooditem['fcid']
			)
		);
		$this->redirect(U('Cart/cart'));
	}

	//点餐完成后提交订单，弹出对话框，
	public function cart(){
		$cartdata = $this->cart->contents ();
		//dump($cartdata);
		$total = $this->cart->total ();
		$total_items = $this->cart->total_items ();

		$this->assign ( 'cartdata', $cartdata );//购物车数据
		$this->assign ( 'total', $total );//购物车总额
		$this->assign ( 'total_items', $total_items );//购物车总额

		$this->display();
	}

	//点餐完成后提交订单，弹出对话框，
	public function index(){
		$cartdata = $this->cart->contents ();
		//dump($cartdata);
		$total = $this->cart->total ();
		$total_items = $this->cart->total_items ();
		$this->assign ( 'cartdata', $cartdata );//购物车数据
		$this->assign ( 'total', $total );//购物车总额
		$this->assign ( 'total_items', $total_items );//购物车总额
		$this->display();
	}

	//购物车更新
	public function cartupdate(){
		$rowid=I('id');
		$cid=I('cid');
		$qtys=$_SESSION['cart_contents'][$rowid]['qty'];
		$qty=$qtys+$cid;
		$status = $this->cart->update ( array (
			'rowid' => $rowid,
			'qty' => $qty
		) );

		header("location:".$_SERVER["HTTP_REFERER"]);
	}

	//购物车清空
	public function cartclear(){
		$this->cart->destroy ();
		header("location:".$_SERVER["HTTP_REFERER"]);
	}

	//输入信息完成后，进成提交成功页
	public function cartcheck(){
		$data['oman']=I('oman');
		$data['otel']=I('otel');
		$data['oraddress']=I('oaddress');
		$data['morecontent']=I('ocontent');
		$cartdata = $this->cart->contents ();
		$total = $this->cart->total ();
		$total_items = $this->cart->total_items ();

		$this->assign ( 'cartdata', $cartdata );//购物车数据
		$this->assign ( 'total', $total );//购物车总额
		$this->assign ( 'total_items', $total_items );//购物车总额
		//dump($data);
		$this->display();
	}

	//订单保存
	public function cartsave(){	
		$Order=D('Foodorder');	
		$data['otel']=I('otel');//联系手机号
		$data['oman']=I('oman');//联系人
		$data['oaddress']=I('oaddress');//联系址址
		$data['order_dtime']=I('orderdtimes');//吃饭时间	
		$data['morecontent']=I('morecontent');//备注	
		if(!$data['oaddress']){$this->message('外卖地址不可以为空');};
		if(!$data['otel']){$this->message('电话不可以为空');};
		$data['orderprice']=$this->cart->total();//购物车商品总价格
		$data['ordercount']=$this->cart->total_items();//购物车商品总数量
		$data['order_ctime']=time();//提交时间

		$cartdata = $this->cart->contents ();


		foreach ($cartdata as $k=>$age) {
			$ages[$k]=Array(
				'fid'=>$age['id'],
				'fname'=>$age['name'],
				'fcount'=>$age['qty'],
				'fprice'=>$age['price'],
				'prices'=>$age['subtotal'],	
			);    
		}
		//将session中的数据转化成数组
		$data['Foodorderext']=$ages;


		$result =$Order->relation(true)->add($data);

		if($result){
			$this->cart->destroy ();
			$this->redirect(U('Cart/cartend'));
		} else {
			$this->message('提交失败');
		}
	}

	public function message($msg = '') {
		$this->assign ( 'msg', $msg );
		$this->display ( 'message' );
		exit ();
	}
}